<?php

namespace app\common;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\TemplateProcessor;

class Word
{

    public function exportTempleteWord($data)
    {


        // 导出word文件路径+文件名
        $exportPath = 'E:\gao\tp6\tp\app\common\simple4.docx';
        // 模板文件路径+文件名
        $templatePath = 'E:\gao\tp6\tp\app\common\templete.docx';

        $templateProcessor = new TemplateProcessor($templatePath);

        $templateProcessor->setValue('User', '成功111');
        $templateProcessor->setValue('user', '成功222');
        $templateProcessor->setValue('test', '成功333');
        $templateProcessor->setValue('Test', '成功444');
        $templateProcessor->setValue('mmm', '成功555');

        //$templateProcessor->setImg('Image',['src' => 'public/img/logo.png']);

        //$templateProcessor->saveAs($exportPath);

        $file_name = 'exportWord.docx';
        //直接下载不存储在服务器上用此代码
        header("Content-Description: File Transfer");
        header('Content-Disposition: attachment; filename="' . $file_name . '"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Expires: 0');
        $templateProcessor->saveAs("php://output");


    }

    public function exportNewWord($data)
    {


        // 导出word文件路径+文件名
        $exportPath = 'E:\gao\tp6\tp\app\common\simple1.docx';
        // 模板文件路径+文件名
        $templatePath = 'E:\gao\tp6\tp\app\common\templete.docx';

        $phpWord = new PhpWord();
        $section = $phpWord->addSection();

        $fontStyleName = 'oneUserDefinedStyle';

        $phpWord->addFontStyle(

            $fontStyleName,

            array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)

        );

        $section->addText(

            '"The greatest accomplishment is not in never falling, '

            . 'but in rising again after you fall." '

            . '(Vince Lombardi)',

            $fontStyleName

        );


        $fontStyle = new \PhpOffice\PhpWord\Style\Font();

        $fontStyle->setBold(true);

        $fontStyle->setName('Tahoma');

        $fontStyle->setSize(13);

        $myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');

        $myTextElement->setFontStyle($fontStyle);


        $objWriter = IOFactory::createWriter($phpWord, 'Word2007');

        $objWriter->save($exportPath);


        return 'success';
    }


}